package org.app.data.temp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class MyIO {
	// String encoding="GBK";
	int capacity = 1024;
	InputStream in;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyIO io = new MyIO();
		String temp = "";
		InputStream in;
		try {
			in = new FileInputStream("D:\\test\\java\\三国志.txt");
			temp=io.open(in);
//			in = new FileInputStream("D:\\test\\java\\1.doc");
//			temp=io.open(in);
//			io.toHex(temp);

			
		//	System.out.println(temp);
			
		//	in = new FileInputStream("D:\\test\\java\\MyCharset.java");
			// temp=io.open(in);
			// temp=io.getEncoding(in);
		//	io.read(in);
		//	System.out.println(temp);
			
		//	in = new FileInputStream("MyTree.java");
			System.out.println(in=new FileInputStream("src\\main\\java\\org\\app\\data\\MyTree.java"));
			//in=MyTree.class.getClassLoader().getResourceAsStream("main/java/org/app/data/MyTree.java");
			//System.out.println("#"+io.getEncoding(in));
		//	io.read(in,"UTF-8");
			//System.out.println(temp);

		//	io.toHex(new String("零".getBytes(),"UTF-8"));

			System.out.println(Integer.MAX_VALUE);
		//	temp+=temp;
			StringBuffer sb=new StringBuffer(temp);
			for(int i=0;i<6;i++){
			sb.append(sb);
			}
			
			//char []c=sb.toString().toCharArray();
		//	System.out.println(c.length);
			System.out.println(sb.length());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void read(InputStream in,String encoding){
		encoding="GBK";
		StringBuffer sb=new StringBuffer();
		try {
			BufferedReader reader=new BufferedReader(new InputStreamReader(in,"ISO-8859-1"));
			String line="";
			while((line=reader.readLine())!=null){
				this.toHex(line);
				System.out.println(line=new String(line.getBytes(),encoding));
				this.toHex(line);
				sb.append(line);
			}
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void read(InputStream in) {

		String encoding = getEncoding(in);

		in = this.in; // /重置
		System.out.println(encoding);

		StringBuffer sb = new StringBuffer();
		int b = -1;
		byte[] temp = new byte[capacity];
		try {
			System.out.println("in" + in.available());
			while ((b = in.read(temp)) != -1) {

				sb.append(new String(temp, 0, b, encoding));
			}
		
			in.close();
			System.out.println(sb);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getEncoding(InputStream in) {
		String encoding = "GB18030";
		long now = System.nanoTime();
		BufferedInputStream bin = new BufferedInputStream(in);

		int b = 0;
		try {
			bin.mark(in.available());
			System.out.println("##" + in.available());
			while ((b = bin.read()) != -1) {
				if ((b >= 0xc0 && b < 0xd0) || (b >= 0xf0)) {
					System.out.println("####");
					// list.add((byte)in.read());///gbk双字节
					break;
				}

				if (b >= 0xe0 && b < 0xf0) {
					int y = in.read();
					int z = in.read();
					if (y >= 0x80 && y < 0xc0 & z >= 0x80 && z < 0xc0) {// 110×
																		// ××××
																		// 10××
																		// ××××
																		// 10××
																		// ××××
						encoding = "UTF-8";
						break;
					} else {
						System.out.println("******");
						// list.add((byte)in.read()); ///gbk双字节
						break;
					}

				}

			}
			System.out.println(in.available() + "*" + bin.available());
			bin.reset();
			this.in = bin; // 重置
			System.out.println(bin.available());

			// in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(encoding + (System.nanoTime() - now));

		return encoding;
	}

	public String open(InputStream in) {

		long now = System.nanoTime();
		int b = -1;
		StringBuffer sb = new StringBuffer();
		List<Byte> list = new ArrayList();
		// String
		String encoding = "GBK";

		try {
			while ((b = in.read()) != -1) {

				list.add((byte) b);
				if ((b >= 0xc0 && b < 0xd0) || (b >= 0xf0)) {
					// i=capacity;//非UTF-8，跳出循环（break）
					list.add((byte) in.read());// /gbk双字节
					break;
				}

				if (b >= 0xe0 && b < 0xf0) {
					int y = in.read();
					int z = in.read();
					list.add((byte) y);
					list.add((byte) z);
					if (y >= 0x80 && y < 0xc0 & z >= 0x80 && z < 0xc0) {// 110×
																		// ××××
																		// 10××
																		// ××××
																		// 10××
																		// ××××
						encoding = "UTF-8";
						break;
					} else {
						// System.out.println("******");
						list.add((byte) in.read()); // /gbk双字节
						break;
					}

				}

			}

			byte[] bt = new byte[list.size()];
			for (int i = 0; i < list.size(); i++) {
				bt[i] = list.get(i);
			}

			sb.append(new String(bt, encoding));
			// System.out.println(new String(bt,encoding)+"("+sb);
			byte[] temp = new byte[capacity];
			while ((b = in.read(temp)) != -1) {
				sb.append(new String(temp, 0, b, encoding));

			}
			in.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// System.out.println(encoding + "\n" + sb);
		System.out.println(System.nanoTime() - now);
		return sb.toString();
	}
	
	public void toHex(String temp){
		char[] ch=temp.toCharArray();
		for(char c:ch){
			System.out.println(Integer.toBinaryString(c));
		}
		
		System.out.println("");
	}

}
